package com.orange.core.pojo;

import com.orange.core.pojo.enums.EErrorCode;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class R<T> implements Serializable {

    private String code = EErrorCode.SUCCESS.getCode();
    private String message = EErrorCode.SUCCESS.getMessage();
    private T result;

    public static R<Boolean> ok() {
        return new R<Boolean>().setResult(true);
    }

    public static <T> R<T> ok(T result) {
        return new R<T>().setResult(result);
    }

    public static <T> R<T> fail(String code, T result) {
        return new R<T>().setCode(code).setResult(result);
    }

    public static <T> R<T> fail(String code, String message) {
        return new R<T>().setCode(code).setMessage(message);
    }

    public static <T> R<T> fail(EErrorCode errorCode) {
        return new R<T>().setCode(errorCode.getCode()).setMessage(errorCode.getMessage());
    }

    public static <T> R<T> fail(EErrorCode errorCode, String message) {
        return new R<T>().setCode(errorCode.getCode()).setMessage(message);
    }

    public static <T> R<T> fail(EErrorCode errorCode, T data) {
        return new R<T>().setCode(errorCode.getCode()).setMessage(errorCode.getMessage()).setResult(data);
    }

    public static <T> R<T> fail(String code, String message, T data) {
        return new R<T>().setCode(code).setMessage(message).setResult(data);
    }

    public static <T> R<T> of(R<?> r, T t) {
        R<T> newR = new R<>();
        return newR.setCode(r.getCode())
                .setMessage(r.getMessage())
                .setResult(t);
    }
}
